﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UI
{
    public partial class FCreateAccount : Form
    {
        private int iAccountID;
        private int iColor;
        public FCreateAccount()
        {
            InitializeComponent();
            lbCurBalance.Text = "账户余额";
            tbNewBalance.Visible = false;
            lbNewBalance.Visible = false;
            lbTip.Visible = false;
            iAccountID = -1;
            iColor = 255;
            setColor(iColor);

        }
        public FCreateAccount(int accountID)
        {
            InitializeComponent();
            this.Text = "修改账户";
            this.iAccountID = accountID;
            lbCurBalance.Text = "当前余额";
            BLL.Account acc = new BLL.Account();
            acc = FMain.accMan.GetAccount(accountID);
            tbAccName.Text = acc.Name.ToString().Trim();
            tbCurBalance.Text = acc.Balance.ToString().Trim();
            tbCurBalance.Enabled = false;
            tbNewBalance.Text = acc.Balance.ToString().Trim();
            iColor = acc.Color;
            setColor(iColor);
        }
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (iAccountID < 0)
            {
                CreateAccount();
            }
            else
            {
                ModifyAccount();
            }
            this.Close();
        }
  

        public int CreateAccount() 
        {
            BLL.Account acc = new BLL.Account();
            int AccountNum = FMain.accMan.AccountList.Count;
            if (tbAccName.Text.ToString().Trim() != "" && tbCurBalance.Text.ToString().Trim() != "") 
            {
                
                try
                {
                    if (AccountNum == 0)
                    {
                        acc.ID = 1;
                    }
                    else 
                    {
                        acc.ID = FMain.accMan.AccountList[AccountNum - 1].ID + 1;
                    }
                    
                    acc.Name = tbAccName.Text.ToString().Trim();
                    acc.Balance = double.Parse(tbCurBalance.Text.ToString().Trim());
                    acc.Color = getColor();
                    acc.CreatedTime = DateTime.Parse(System.DateTime.Now.ToString("d"));
                    acc.ModifiedTime = DateTime.Parse(System.DateTime.Now.ToString("d"));
                    if (FMain.accMan.IsNameExist(acc)) 
                    {
                        return 2;
                    }
                    FMain.accMan.AddAccount(acc);
                }
                catch 
                {
                    return 3;
                }
                return 1;
            }
            return 0;
            
        }
        public int ModifyAccount() 
        {
            BLL.Account acc = new BLL.Account();
            acc = FMain.accMan.GetAccount(iAccountID);
            double dCurBalance = double.Parse(tbCurBalance.Text.ToString().Trim());
            if (tbAccName.Text.ToString().Trim() != "" )
            {

                try
                {
                    acc.Name = tbAccName.Text.ToString().Trim();
                    if (tbNewBalance.Text.ToString().Trim() == "" || tbCurBalance.Text.ToString().Trim() == tbNewBalance.Text.ToString().Trim())
                    {
                        acc.Balance = dCurBalance;
                    }
                    else 
                    {
                        double dNewBalance = double.Parse(tbNewBalance.Text.ToString().Trim());
     
                        if (!CreateRecordForBal(dNewBalance, dCurBalance))
                        {
                            return 2;   
                        }
                        acc.Balance = dNewBalance;
                    }
                    acc.Color = getColor();
                    acc.ModifiedTime = DateTime.Parse(System.DateTime.Now.ToString("d"));
                    FMain.accMan.UpdateAccount(acc);
                }
                catch
                {
                    return 2;
                }
                return 1;
            }
            return 0;
            
            
        }
        //public int getColor(int index) 
        //{
        //    Color[] colorlist={Color.Black,Color.Blue,Color.Green,Color.Red};
        //    return colorlist[index].ToArgb();  
        //}
        //public int getColorIndex(int iColorArgb) 
        //{
        //    Color[] colorlist = { Color.Black, Color.Blue, Color.Lime, Color.Red};
        //    int index=0;
        //    while (colorlist.Count() != index+1) 
        //    {
        //        if (colorlist[index].ToArgb() == iColorArgb) break;
        //        index++;
        //    }
        //    return index;
        //}

        /// <summary>
        /// 余额调整时创建对账收入/支出记录，创建成功输入true，反之为false
        /// </summary>
        public bool CreateRecordForBal(double newBalance, double curBalance)
        {
            double income = newBalance - curBalance;
            BLL.Record rec = new BLL.Record();
            try
            {
                int recordCount = FMain.rcdMan.RecordList.Count;
                if (recordCount == 0)
                {
                    rec.ID = 1;
                }
                else
                {
                    rec.ID = FMain.rcdMan.RecordList[recordCount - 1].ID + 1;
                }
                rec.Time = DateTime.Parse(System.DateTime.Now.ToString("d"));
                rec.AccountID = iAccountID;
                if (income > 0)
                {
                    rec.TypeID = 7;//对账收入
                    rec.Amount = income;
                }
                else
                {
                    rec.TypeID = 8;//对账支出
                    rec.Amount = -income;

                }
                FMain.rcdMan.AddRecord(rec);
            }
            catch
            {
                return false;
            }
            return true;
        }
        private int getColor()
        {
            foreach (Control rb in this.groupBox1.Controls)
            {
                if (rb is RadioButton)
                {
                    if(((RadioButton)rb).Checked)
                         iColor = ParseRGB(rb.BackColor);
                }
            }
            return iColor;
        }
        private void setColor(int value)
        {
            foreach (Control rb in this.groupBox1.Controls)
            {
                if (rb is RadioButton)
                {
                    if (ParseRGB(rb.BackColor) == value)
                        ((RadioButton)rb).Checked = true;
                }
            }
        }
        private int ParseRGB(Color color)
        {
            return (int)(((uint)color.A << 24) | ((uint)color.R << 16) | (ushort)(((ushort)color.G << 8) | color.B));
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

       
       
    }
}
